home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_300
/
367_01
/
futi14as.zoo
/
cp.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-02-22
|
23KB
|
954 lines
/* cp.c -- file copying (main routines)
Copyright (C) 1989, 1990 Free Software Foundation.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Written by Torbjorn Granlund and David MacKenzie. */
/* MS-DOS port (c) 1990 by Thorsten Ohl, ohl@gnu.ai.mit.edu
This port is also distributed under the terms of the
GNU General Public License as published by the
Free Software Foundation.
Please note that this file is not identical to the
original GNU release, you should have received this
code as patch to the official release. */
static char RCS_Id[] =
"$Header: e:/gnu/fileutil/RCS/cp.c 1.4.0.2 90/09/19 11:18:06 tho Exp $";
static char Program_Id[] = "cp";
static char RCS_Revision[] = "$Revision: 1.4.0.2 $";
#define VERSION \
"GNU %s, Version %.*s (compiled %s %s for MS-DOS)\n", Program_Id, \
(sizeof RCS_Revision - 14), (RCS_Revision + 11), __DATE__, __TIME__
#define COPYING \
"This is free software, distributed under the terms of the\n" \
"GNU General Public License. For details, see the file COPYING.\n"
/* Yet to be done:
* Symlink translation. */
#include <stdio.h>
#include <getopt.h>
#include "cp.h"
#include "backupfile.h"
#ifdef MSDOS
#include <assert.h>
extern enum backup_type get_version (char *version);
extern char *savedir (char *dir, unsigned name_size);
#define strip_trailing_slashes(path) strip_trailing_slashes (&path)
#define is_ancestor(statb, ancestors) 0
#define hash_init(module, size)
#define remember_copied(path, ino, dev) NULL
#define remember_created(path) 0
#define forget_all()
char new_file;
#endif
#ifdef MKFIFO_MISSING
/* This definition assumes that MODE has the S_IFIFO bit set. */
#define mkfifo(path, mode) (mknod ((path), (mode), 0))
#endif
enum backup_type get_version ();
int eaccess_stat ();
/* Initial number of entries in each hash table entry's table of inodes. */
#define INITIAL_HASH_MODULE 100
/* Initial number of entries in the inode hash table. */
#define INITIAL_ENTRY_TAB_SIZE 70
/* A pointer to either lstat or stat, depending on
whether dereferencing of symlinks is done. */
#ifdef MSDOS
int (*xstat) (char *, struct stat *);
#else
int (*xstat) ();
#endif
/* The invocation name of this program. */
char *program_name;
/* If nonzero, copy all files except directories and, if not dereferencing
them, symbolic links, as if they were regular files. */
int flag_copy_as_regular = 1;
/* If nonzero, dereference symbolic links (copy the files they point to). */
int flag_dereference = 1;
/* If nonzero, remove existing target nondirectories. */
int flag_force = 0;
/* If nonzero, query before overwriting existing targets with regular files. */
int flag_interactive = 0;
/* If nonzero, give the copies the original files' permissions,
ownership, and timestamps. */
int flag_preserve = 0;
/* If nonzero, copy directories recursively and copy special files
as themselves rather than copying their contents. */
int flag_recursive = 0;
/* If nonzero, when copying recursively, skip any subdirectories that are
on different filesystems from the one we started on. */
int flag_one_file_system = 0;
/* If nonzero, do not copy a nondirectory that has an existing destination
with the same or newer modification time. */
int flag_update = 0;
/* If nonzero, display the names of the files before copying them. */
int flag_verbose = 0;
/* The error code to return to the system. */
int exit_status = 0;
/* The bits to preserve in created files' modes. */
int umask_kill;
struct option long_opts[] =
{
#ifdef MSDOS
{"copying", 0, NULL, 30},
{"version", 0, NULL, 31},
#endif
{"backup", 0, NULL, 'b'},
{"force", 0, NULL, 'f'},
{"interactive", 0, NULL, 'i'},
{"no-dereference", 0, &flag_dereference, 0},
{"one-file-system", 0, &flag_one_file_system, 1},
{"preserve", 0, &flag_preserve, 1},
{"recursive", 0, NULL, 'R'},
{"suffix", 1, NULL, 'S'},
{"update", 0, &flag_update, 1},
{"verbose", 0, &flag_verbose, 1},
{"version-control", 1, NULL, 'V'},
{NULL, 0, NULL, 0}
};
void
main (argc, argv)
int argc;
char *argv[];
{
int c;
int ind;
int make_backups = 0;
char *version;
program_name = argv[0];
version = getenv ("SIMPLE_BACKUP_SUFFIX");
if (version)
simple_backup_suffix = version;
version = getenv ("VERSION_CONTROL");
/* Find out the current file creation mask, to knock the right bits
when using chmod. The creation mask is set to to be liberal, so
that created directories can be written, even if it would not
have been allowed with the mask this process was started with. */
#ifdef MSDOS /* not 100%ly correct ... */
umask_kill = 07777 ^ umask (0);
#else
umask_kill = 0777777 ^ umask (0);
#endif
while ((c = getopt_long (argc, argv, "bdfipruvxRS:V:", long_opts, &ind))
!= EOF)
{
switch (c)
{
case 0:
break;
case 'b':
make_backups = 1;
break;
case 'd':
flag_dereference = 0;
break;
case 'f':
flag_force = 1;
flag_interactive = 0;
break;
case 'i':
flag_force = 0;
flag_interactive = 1;
break;
case 'p':
flag_preserve = 1;
break;
case 'r':
flag_recursive = 1;
flag_copy_as_regular = 1;
break;
case 'R':
flag_recursive = 1;
flag_copy_as_regular = 0;
break;
case 'u':
flag_update = 1;
break;
case 'v':
flag_verbose = 1;
break;
case 'x':
flag_one_file_system = 1;
break;
case 'S':
simple_backup_suffix = optarg;
break;
case 'V':
version = optarg;
break;
#ifdef MSDOS
case 30:
fprintf (stderr, COPYING);
exit (0);
break;
case 31:
fprintf (stderr, VERSION);
exit (0);
break;
#endif
default:
usage ((char *) 0);
}
}
if (make_backups)
backup_type = get_version (version);
if (flag_preserve == 1)
#ifdef MSDOS /* not 100%ly correct ... */
umask_kill = 07777;
#else
umask_kill = 0777777;
#endif
/* The key difference between -d (+no-dereference) and not is the version
of `stat' to call. */
if (flag_dereference)
xstat = stat;
else
xstat = lstat;
/* Allocate space for remembering copied and created files. */
hash_init (INITIAL_HASH_MODULE, INITIAL_ENTRY_TAB_SIZE);
exit_status |= do_copy (argc, argv);
exit (exit_status);
}
/* Scan the arguments, and copy each by calling copy.
Return 0 if successful, 1 if any errors occur. */
int
do_copy (argc, argv)
int argc;
char *argv[];
{
char *target;
struct stat sb;
int new_dst = 0;
int ret = 0;
if (optind >= argc)
usage ("missing file arguments");
if (optind >= argc - 1)
usage ("missing file argument");
target = argv[argc - 1];
strip_trailing_slashes (target);
if (lstat (target, &sb))
{
if (errno != ENOENT)
{
error (0, errno, "%s", target);
return 1;
}
else
new_dst = 1;
}
else
{
struct stat sbx;
/* If `target' is not a symlink to a nonexistent file, use
the results of stat instead of lstat, so we can copy files
into symlinks to directories. */
if (stat (target, &sbx) == 0)
sb = sbx;
}
if (!new_dst && (sb.st_mode & S_IFMT) == S_IFDIR)
{
/* cp e_file_1...e_file_n e_dir
copy the files `e_file_1' through `e_file_n